package com.koron.standard.riskSources.controller;


import com.koron.bean.base.Response;
import com.koron.standard.riskSources.bean.po.RiskItem;
import com.koron.standard.riskSources.service.RiskItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业风险(RiskItem)表控制层
 *
 * @author jzp
 * @since 2021-09-16
 */
@Api(tags = "作业风险项接口")
@RestController
@RequestMapping("riskItem")
public class RiskItemController {

    @Autowired
    private RiskItemService riskItemService;

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<RiskItem> select(@RequestParam String id) {
        RiskItem riskItem = riskItemService.getById(id);
        return Response.ok(riskItem);
    }

    @ApiOperation(value = "根据风险源id查询风险项")
    @GetMapping("listItems")
    public Response<List<RiskItem>> listItems(@RequestParam("id") String sourceId) {
        List<RiskItem> riskSources = riskItemService.listBySourceId(sourceId);
        return Response.ok(riskSources);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody RiskItem riskItem) {
        Assert.hasText(riskItem.getSourceId(), "请先关联风险源");
        riskItemService.save(riskItem);
        return Response.ok();
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody RiskItem riskItem) {
        riskItemService.updateById(riskItem);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam String id) {
        riskItemService.removeById(id);
        return Response.ok();
    }
}
